package gms.actions.um;

import gms.actions.SuperAction;
import gms.entities.um.Role;
import gms.entities.um.RolePermission;
import gms.entities.um.User;
import gms.service.UmUtil;
import gms.service.UserManagementService;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.servlet.ServletException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ModelDriven;

@Component
public class UserManagementAction extends SuperAction implements
		ModelDriven<User>{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private int pageSize = 10; 
	private User user;
	private Integer userRoleId;

	@Autowired
	private UserManagementService userManagementService;

	//���ڶ�user������й淶������
	@Autowired
	private UmUtil umUtil;
	
	//�û���¼
	public String login() throws IOException {
		
		if (user != null) {
			User loginUser = userManagementService.getUser(user);
			if(loginUser!=null){
				//��ŵ�¼���û�����Ϣ
				session.setAttribute("user", loginUser);
				//��ȡ�û�Ȩ�ޣ����ڶ�����ҳ��ͼ
				List<String> rolePermission_name_list = new ArrayList<>();
				for(RolePermission rp : loginUser.getRole().getRps())
					rolePermission_name_list.add(rp.getPermissionName());
				session.setAttribute("rolePermission_name_list", rolePermission_name_list);
				return "success";
			}
		} 
		
		//���ݿ����޼�¼����¼ʧ��
		request.setAttribute("loginErrorMsg", "信息错误");
		return "login";
		
	}
	
	//�����û�������Ϣ�޸�����
	public String selfMsgModif(){
		return "modifi";
	}	
	
	//�����û�������Ϣ�޸ĵ��ύ
	public String selfMsgModifCommit(){
		//��Ҫ�Ȼ�ȡ���û��Ľ�ɫ,���ý�ɫ��
		User proUser = userManagementService.getUser(user.getUserId());
		user.setRole(proUser.getRole());
		
		//�����û��µ���Ϣ
		userManagementService.updateUser(user);
		
		//������ҳ  ���¸�����Ϣ
		session.setAttribute("user", user);
		return "success";
	}
	
	//�����û���Ϣ
	public String saveUser() throws ServletException, IOException {
		userManagementService.saveUser(user);
		return "login";
	}
	
	
	//�û���������
	public String userManage(){
		//��ȡ����ϵͳ���н�ɫ
		List<Role> role_list = userManagementService.getAllRole();
		session.setAttribute("role_list", role_list);
		//����û���
		session.setAttribute("user_list", null);
		return "userManage";
	}
	
	//�û���ѯ
	public String userQuery(){
		//����ѯ��Ϣ����request�����ڲ�ѯ�����Ļ���
		session.setAttribute("query_user", user);
		
//		//��user���й淶������
		user = umUtil.transUser(user);
		int userTotal = userManagementService.getQueryNum(user);
		session.setAttribute("userTotal", userTotal);
		System.out.println(userTotal);
		//ִ���û���ѯ
		//Ĭ�ϴӵ�һҲ��ʼ
		List<User> user_list = userManagementService.getUserWithPage(user, 1, pageSize);
		System.out.println(user_list);
		
		session.setAttribute("user_list", user_list);
		
		return "userManage";
	}
	
	//�ж��û����Ƿ���ڣ�����Ajax��֤
	public void checkName() throws IOException {
		String userName = request.getParameter("checkUserName");
		String result = null;
		if (userManagementService.registeredCheck(userName))
			result = "<font color='green' size='2'>可用</font>";
		else
			result = "<font color='red' size='2'>已注册</font>";
		
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		response.getWriter().print(result);
		System.out.println(result);
	}
	
	public void queryWithPage() throws IOException{
		int pageNo = Integer.parseInt(request.getParameter("pageNo"));
		User user = (User) session.getAttribute("query_user");
		user = UmUtil.transUser(user);
		
		List<User> user_list = userManagementService.getUserWithPage(user, pageNo, pageSize);
//		session.setAttribute("user_list", user_list);
		
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		
		String str = umUtil.getHtmlStr(user_list);
		response.getWriter().print(str);
		
	}
	
	//ɾ���û�
	public String userDelete(){
		Integer userId = Integer.parseInt(request.getParameter("deleteUserId"));
		userManagementService.deleteUser(userId);
		
		//����������1
		int userTotal = (int) session.getAttribute("userTotal");
		session.setAttribute("userTotal", --userTotal);
		
		User queryUser = (User) session.getAttribute("query_user");
		queryUser = UmUtil.transUser(queryUser);
		List<User> user_list = userManagementService.getUserWithPage(queryUser, 1, pageSize);
		session.setAttribute("user_list", user_list);
		
		return "userManage";
	}
	
	//�����޸ĺ���û�
	public String userMsgModif(){
		
		Role role = new Role(); 
		role.setRoleId(userRoleId);
		user.setRole(role);
		
		//�����û��µ���Ϣ
		userManagementService.updateUser(user);
		
		User queryUser = (User) session.getAttribute("query_user");
		queryUser = UmUtil.transUser(queryUser);
		List<User> user_list = userManagementService.getUserWithPage(queryUser, 1, pageSize);
		session.setAttribute("user_list", user_list);
		
		return "userManage";
		
	}
	
	//��ȫ�˳�
	public String userExit(){
		//ע��session
		session.invalidate();
		return "login";
	}
	
	@Override
	public User getModel() {
		// TODO Auto-generated method stub
		if (user == null)
			user = new User();
		return user;
	}

	public Integer getUserRoleId() {
		return userRoleId;
	}
	
	public void setUserRoleId(Integer userRoleId) {
		this.userRoleId = userRoleId;
	}
}
